﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections;

namespace LCWMS
{
    public partial class w_so002_JS : DockContent
    {
        private DataSet dscustomer;
        //这里实例化一个DateTimePicker控件   
        DateTimePicker dtp = new DateTimePicker();
        Rectangle _Rectangle;

        public w_so002_JS()
        {
            InitializeComponent();
            dg_delivery.AutoGenerateColumns = false;
            dgv_JZ.AutoGenerateColumns = false;

            //把时间控件加入DataGridView   
            dg_delivery.Controls.Add(dtp); 
            //单显示时间  
            dtp.Visible = false;
            dtp.Format = DateTimePickerFormat.Custom;
            dtp.CustomFormat = "HH:mm";
            dtp.MouseLeave += new EventHandler(dtp_MouseLeave);
        }

        private void dtp_MouseLeave(object sender, EventArgs e)
        {
            ////时间控件选择时间时，就把时间赋给所在的单元格   
            //if (string.IsNullOrEmpty(dg_delivery.CurrentRow.Cells["need_time"].Value.ToString()))
            //{
            //    string s_time = dg_delivery.CurrentRow.Cells["so_day"].Value + " " + dtp.Text.ToString();
            //    DateTime dt_time = Convert.ToDateTime(s_time);
            //    dg_delivery.CurrentRow.Cells["need_time"].Value = dt_time;  
            //}
            //else
            //{
            //    dg_delivery.CurrentRow.Cells["need_time"].Value = dtp.Text.ToString();  
            //}
            dg_delivery.CurrentRow.Cells["need_time"].Value = dtp.Text.ToString();
            dtp.Visible = false;
        }


        private void dg_delivery_Scroll(object sender, ScrollEventArgs e)
        {
            dtp.Visible = false;
        }

        private void dg_delivery_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
        {
            dtp.Visible = false;
        }

        private void w_so002_JS_Load(object sender, EventArgs e)
        {
            DataSet f_ds = new DataSet();
            //初始化变量
            dscustomer = new DataSet();
           
            //初始化货主编码 
            try
            {
                dscustomer = main.g_class.comm_get_consignor();
                cbx_customer.ValueMember = "consignor";
                cbx_customer.DisplayMember = "consignor";
                DataRow drnew;
                drnew = dscustomer.Tables[0].NewRow();
                drnew["consignor"] = "";
                drnew["consignor_name"] = "";
                dscustomer.Tables[0].Rows.Add(drnew);
                cbx_customer.DataSource = dscustomer.Tables[0];
                //cbx_customer.Text = "";
                //tb_cusname.Text = "";
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get Consignor's Information", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }
            //初始单据类型
            try
            {
                f_ds = main.g_class.comm_get_dictionary("so_type");
                //进行语言转换
                int i_ds;
                i_ds = f_ds.Tables[0].Rows.Count;
                if (i_ds > 0)
                {
                    for (int i = 0; i < i_ds; i++)
                    {
                        f_ds.Tables[0].Rows[i]["so_type"] = main.g_class.comm_getchangestring(f_ds.Tables[0].Rows[i]["so_type"].ToString().Trim(), main.g_info_dataset);
                    }
                }
                DataRow drnew;
                drnew = f_ds.Tables[0].NewRow();
                drnew["so_type"] = "";
                f_ds.Tables[0].Rows.Add(drnew);
                cbx_sotype.ValueMember = "so_type";
                cbx_sotype.DisplayMember = "so_type";
                cbx_sotype.DataSource = f_ds.Tables[0];
                cbx_sotype.Text = "";
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize Information of bill's type", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }
            //日期控件默认从今天到一个星期
            //dtp_start.CustomFormat = "yyyy.MM.dd HH:mm:ss";
            dtp_start.CustomFormat = "yyyy.MM.dd";
            dtp_start.Format = DateTimePickerFormat.Custom;
            dtp_end.CustomFormat = "yyyy.MM.dd";
            dtp_end.Format = DateTimePickerFormat.Custom;
            dtp_start.Value = System.Convert.ToDateTime(DateTime.Now.ToString("yyyy.MM.dd").Trim());
            dtp_end.Value = System.Convert.ToDateTime(DateTime.Now.AddDays(7).ToString("yyyy.MM.dd").Trim());
            //初始化之后，改变语言
            //语言转换
            main.g_class.comm_changeControl(this, main.g_win_dataset);
        }

        private void cbx_customer_TextChanged(object sender, EventArgs e)
        {
            string str_cust;
            str_cust = cbx_customer.Text.Trim();
            tb_cusname.Text = main.g_class.comm_findname(str_cust, dscustomer);
        }

        private void btn_query_Click(object sender, EventArgs e)
        {
            string str_sql;
            DataSet ds_query;

            //根据条件提取出库单数据
            if (main.dbtype == "SQL")
            {
                str_sql = "select distinct '1' check1,b.need_time,getdate(),a.eas_no,a.project_name,a.consignor,a.so_no,a.customer_sale_no,a.customer_code,a.original_order_no,a.so_day,case a.rush_flag when '0' then '" + main.g_class.comm_getchangestring("Normal", main.g_info_dataset) + "' " +
                         "else '" + main.g_class.comm_getchangestring("Instancy", main.g_info_dataset) + "' end as rush_flag,a.so_prior,case a.so_type when '0' then '" + main.g_class.comm_getchangestring("Outbound", main.g_info_dataset) + "' " +
                         "when '1' then '" + main.g_class.comm_getchangestring("Return", main.g_info_dataset) + "' when '2' then '" + main.g_class.comm_getchangestring("Consignment", main.g_info_dataset) + "' when '3' then '" +
                          main.g_class.comm_getchangestring("Appropriation", main.g_info_dataset) + "' when '4' then '" + main.g_class.comm_getchangestring("Sale", main.g_info_dataset) + "' else '" +
                          main.g_class.comm_getchangestring("Other", main.g_info_dataset) + "' end as so_type,convert(char(1),floor(a.standby_5)) standby_5,a.maker,a.made_time,c.warehouseman,a.customer_name,b.lot_attribute11,b.lot_attribute6,b.so_line_no from " + main.g_space + ".so_ship_header a," + main.g_space + ".so_ship_detail b," + main.g_space + ".mst_sku c where a.inv_org = '" + main.g_org + "' and a.so_status ='0' and a.standby_4 = 1 and a.so_flag = '0' and a.wave_flag='0' and a.cross_flag='0' and ";
            }
            else
            {
                str_sql = "select distinct '1' check1,sysdate,a.eas_no,a.project_name,a.consignor,a.so_no,a.customer_sale_no,a.customer_code,a.original_order_no,a.so_day,case a.rush_flag when '0' then '" + main.g_class.comm_getchangestring("Normal", main.g_info_dataset) + "' " +
                         "else '" + main.g_class.comm_getchangestring("Instancy", main.g_info_dataset) + "' end as rush_flag,a.so_prior,case a.so_type when '0' then '" + main.g_class.comm_getchangestring("Outbound", main.g_info_dataset) + "' " +
                         "when '1' then '" + main.g_class.comm_getchangestring("Return", main.g_info_dataset) + "' when '2' then '" + main.g_class.comm_getchangestring("Consignment", main.g_info_dataset) + "' when '3' then '" +
                          main.g_class.comm_getchangestring("Appropriation", main.g_info_dataset) + "' when '4' then '" + main.g_class.comm_getchangestring("Sale", main.g_info_dataset) + "' else '" +
                          main.g_class.comm_getchangestring("Other", main.g_info_dataset) + "' end as so_type,to_char(a.standby_5) standby_5,a.maker,a.made_time,c.warehouseman,a.customer_name from " + main.g_space + ".so_ship_header a," + main.g_space + ".so_ship_detail b," + main.g_space + ".mst_sku c where a.inv_org = '" + main.g_org + "' and a.so_status ='0' and a.standby_4 = 1 and a.so_flag = '0' and a.wave_flag='0' and a.cross_flag='0' and ";
            }
            str_sql += " a.inv_org=b.inv_org and a.consignor=b.consignor and a.so_no=b.so_no and b.inv_org=c.inv_org and b.consignor=c.consignor and b.sku=c.sku and c.sku_class='01' and ";
            //判断是否有货主
            if (cbx_customer.Text.Trim() != "")
            {
                str_sql = str_sql + "a.consignor = '" + cbx_customer.Text.Trim() + "' and ";
            }
            else //没有货主提取有权限的所有货主
            {
                str_sql = str_sql + "a.consignor in (select a.consignor from " + main.g_space + ".mst_customer a," + main.g_space + ".sys_user b," + main.g_space + ".sys_user_customer c" +
                           " where b.inv_org = c.inv_org and b.user_id = c.user_id and c.inv_org = a.inv_org and c.consignor = a.consignor and b.inv_org = '" + main.g_org + "' and b.user_id = '" + main.g_user + "' and a.consignor_flag='1') and ";
            }
            string str_sotype;
            //判断订单类型
            str_sotype = main.g_class.comm_getconversestring(cbx_sotype.Text.Trim(), main.g_info_dataset);
            switch (str_sotype.Trim().ToUpper())
            {
                case "OUTBOUND":
                    str_sotype = "0";
                    break;
                case "RETURN":
                    str_sotype = "1";
                    break;
                case "CONSIGNMENT":
                    str_sotype = "2";
                    break;
                case "APPROPRIATION":
                    str_sotype = "3";
                    break;
                case "SALE":
                    str_sotype = "4";
                    break;
                case "OTHER":
                    str_sotype = "5";
                    break;
                default:
                    str_sotype = "*";
                    break;
            }
            if (str_sotype != "*")
            {
                str_sql = str_sql + "a.so_type = '" + str_sotype + "' and ";
            }
            //判断订单号
            if (tb_so.Text.Trim() != "")
            {
                str_sql = str_sql + "a.so_no like '%" + tb_so.Text.Trim() + "%' and ";
            }
            //判断客户单号
            if (tb_eas_no.Text.Trim() != "")
            {
                str_sql = str_sql + "a.eas_no like '%" + tb_eas_no.Text.Trim() + "%' and ";
            }
            //判断原订单号
            if (tb_standby_13.Text.Trim() != "")
            {
                str_sql = str_sql + "a.standby_13 like '%" + tb_standby_13.Text.Trim() + "%' and ";
            }
            
            //去掉多余的and, 并且加上order 语句
            str_sql = str_sql.Substring(0, str_sql.Length - 4);
            
            str_sql = str_sql + " order by a.so_no,b.lot_attribute11,b.lot_attribute6";
            //提取数据
            try
            {
                if (main.dbtype == "SQL")
                {
                    ds_query = main.dbservice.Sql_exec_selectsql(str_sql);
                }
                else
                {
                    ds_query = main.dbservice.oracle_exec_selectsql(str_sql);
                }
                //加载数据
                dg_delivery.DataSource = ds_query.Tables[0];
                dgv_JZ.DataSource = ds_query.Tables[0];
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get bill's Information", main.g_info_dataset) + "!" + ee.ToString());

            }
            
        }

        private void dg_delivery_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string str_name;
            int i_temp;
            if (dg_delivery.CurrentRow != null)  //说明选择了行号
            {
                i_temp = dg_delivery.CurrentRow.Index;
                //判断点击的列是否是check
                str_name = dg_delivery.Columns[dg_delivery.CurrentCell.ColumnIndex].Name.ToString().Trim();
                if (str_name == "check1")
                {
                    if (dg_delivery.EditMode == DataGridViewEditMode.EditProgrammatically)
                    {
                        dg_delivery.EditMode = DataGridViewEditMode.EditOnKeystroke;
                        if ((bool)dg_delivery.Rows[i_temp].Cells["check1"].EditedFormattedValue)
                            dg_delivery.Rows[i_temp].Cells["check1"].Value = false;
                        else
                            dg_delivery.Rows[i_temp].Cells["check1"].Value = true;
                    }
                }
                else
                {
                    dg_delivery.EditMode = DataGridViewEditMode.EditProgrammatically;
                }

                //时间控件显示操作
                if (e.RowIndex >= 0)
                {
                    if (e.ColumnIndex == 1)
                    {
                        _Rectangle = dg_delivery.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); //得到所在单元格位置和大小   
                        dtp.Size = new Size(_Rectangle.Width, _Rectangle.Height); //把单元格大小赋给时间控件   
                        dtp.Location = new Point(_Rectangle.X, _Rectangle.Y); //把单元格位置赋给时间控件   
                        dtp.Visible = true;  //可以显示控件了   
                    }
                }
            }
        }

        //全选
        private void btn_all_Click(object sender, EventArgs e)
        {
            if (dg_delivery.CurrentRow != null)
            {
                for (int i = 0; i < dg_delivery.Rows.Count; i++)
                {
                    dg_delivery.Rows[i].Cells["check1"].Value = "1";
                }
            }
        }

        //反选
        private void btn_reset_Click(object sender, EventArgs e)
        {
            if (dg_delivery.CurrentRow != null)
            {
                for (int i = 0; i < dg_delivery.Rows.Count; i++)
                {
                    dg_delivery.Rows[i].Cells["check1"].Value = "0";
                }
            }
        }

        //拉动
        private void btn_pull_Click(object sender, EventArgs e)
        {
            if (dg_delivery.CurrentRow != null)
            {
                //public string Sql_exec_proc_pullsku(string s_org, string s_consignor, string s_so_no, string s_user)
                ArrayList al_sql = new ArrayList();
                string s_retrun = "";
                string s_so_no = "";
                DateTime time = new DateTime();
                string s_customer = "";
                for (int i = 0; i < dg_delivery.Rows.Count; i++)
                {
                    if (dg_delivery.Rows[i].Cells["check1"].Value.ToString().Trim() == "1")
                    {
                        if (string.IsNullOrEmpty(dg_delivery.Rows[i].Cells["need_time"].Value.ToString().Trim()))
                        {
                            MessageBox.Show("请先选择时间!");
                            return;
                        }

                        //货主判断是否一致
                        if (string.IsNullOrEmpty(s_customer))
                        {
                            s_customer = dg_delivery.Rows[i].Cells["consignor"].Value.ToString().Trim();
                        }
                        else
                        {
                            if (s_customer.Trim() != dg_delivery.Rows[i].Cells["consignor"].Value.ToString().Trim())
                            {
                                MessageBox.Show("库存无法拉动，因为货主不一致!");
                                return;
                            }
                        }
                        time = Convert.ToDateTime(dg_delivery.Rows[i].Cells["need_time"].Value.ToString().Trim());
                        al_sql.Add("update " + main.g_space + ".so_ship_detail set need_time='" + time + "' where consignor = '" + cbx_customer.Text.Trim() + "' and so_no='" + dg_delivery.Rows[i].Cells["so_no"].Value.ToString().Trim() + "' and so_line_no='" + dg_delivery.Rows[i].Cells["so_line_no"].Value.ToString().Trim() + "'");

                        s_so_no += "'" + dg_delivery.Rows[i].Cells["so_no"].Value.ToString().Trim() + "',";
                    }
                }
                if (s_so_no.Length > 0)
                {
                    s_so_no = s_so_no.Substring(0, s_so_no.Length - 1);
                    //更新时间
                    string[] sl_sql = (string[])al_sql.ToArray(typeof(string));

                    if (main.dbtype == "SQL")
                    {
                        main.dbservice.Sql_exec_mutilsql(sl_sql);
                    }
                    else
                    {
                        main.dbservice.oracle_exec_mutilsql(sl_sql);
                    }

                    //拉动
                    s_retrun = main.dbservice.Sql_exec_proc_pullsku(main.g_org, s_customer, s_so_no, main.g_user);
                    if (s_retrun == "0")
                    {
                        MessageBox.Show("拉动成功!");
                    }
                    else
                    {
                        MessageBox.Show(s_retrun);
                    }
                }
                else
                {
                    MessageBox.Show("请先勾选单据");
                }
            }
        }

       
    }
}
